package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.SystemLog;
import com.jw.blog.utils.ConstantUtil;
import com.jw.blog.wblog.entity.Links;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.ILinksService;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className LinksControllerAdmin
 * @date 2020/6/21 15:04
 **/
@RestController
public class LinksControllerAdmin {

    @Resource
    private ILinksService iLinksService;

    @Data
    static
    class StatusCount implements Serializable {
        private static final long serialVersionUID = 1L;
        //写个注释吧，不写我怕我自己也记不住，害，这是个啥来着，
        // 容我。组织一下语言，这个内部类的一个属性，妈耶你这不是废话么。
        //状态标签，1为正文，-1为草稿，2为回收站
        private Integer status;
        //不同状态的博客数量
        private Integer count;
    }


    /**
     * 未处理的友链数
     *
     * @return 数字
     */
    @GetMapping("/admin/selectNoApplyLinksCount")
    public ResultBean<Integer> selectNoApplyLinksCount() {
        QueryWrapper<Links> linksQueryWrapper = new QueryWrapper<>();
        linksQueryWrapper.lambda().eq(Links::getIsApply, -1);
        int count = iLinksService.count(linksQueryWrapper);
        return new ResultBean<>(count);
    }

    @PostMapping("/admin/selectLikeLinksListByPage")
    public ResultBean<IPage<Links>> selectLikeLinksListByPage(Links links,
                                                  @RequestParam(value = "page") Integer page,
                                                  @RequestParam(value = "pageSize") Integer pageSize) {
        QueryWrapper<Links> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(links.getName())) {
            wrapper.lambda().like(Links::getName, links.getName());
        }
        if (links.getStatus() != null && links.getStatus() != 0) {
            wrapper.lambda().eq(Links::getStatus, links.getStatus());
        }
        if (links.getIsApply() != null) {
            wrapper.lambda().eq(Links::getIsApply,links.getIsApply());
        }
        wrapper.lambda().orderByDesc(Links::getAddTime);
        //分页显示
        Page<Links> linksPage = new Page<>();
        linksPage.setCurrent(page);
        linksPage.setSize(pageSize);
        ResultBean<IPage<Links>> resultBean = new ResultBean<>();
        Page<Links> page1 = iLinksService.page(linksPage, wrapper);
        if (page1.getRecords().size() != 0){
            resultBean.setCode(ResultBean.SUCCESS);
            resultBean.setData(page1);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    @PostMapping("/admin/selectLinksByIdAdmin")
    public ResultBean<Links> selectLinksByIdAdmin(@RequestParam(value = "id") Integer id) {
        return new ResultBean<>(iLinksService.getById(id));
    }

    /**
     * 实现删除友链的功能
     * @param prarm 。。。
     * @param id。。。。
     * @return 返回一个状态码，这个也需要改正，后期我会改返回统一 的状态码供前端来判断
     */
    @PostMapping("/admin/deleteLinks")
    @SystemLog(description = ConstantUtil.LINKS_DELETE,userType=ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteLinks(String prarm, Integer id)  {
        ResultBean<Object> resultBean = new ResultBean<>();
        if (iLinksService.removeById(id)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：删除失败
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    /**
     * 实现添加友链的功能
     * @param prarm 这个参数暂时不知到是干什么的，貌似是为了操作记录操作日志的参数
     * @param links 传入的东西
     * @return 返回的东西
     */
    @PostMapping("/admin/addLinks")
    @SystemLog(description = ConstantUtil.LINKS_ADD,userType= ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addLinks(String prarm, Links links) {
        ResultBean<Object> resultBean = new ResultBean<>();
        links.setAddTime(LocalDateTime.now());
        if (iLinksService.save(links)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：插入失败
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    @PostMapping("/admin/updateLinks")
    @SystemLog(description = ConstantUtil.LINKS_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateLinks(String prarm,Links links) {
        ResultBean<Object> resultBean = new ResultBean<>();
        if (iLinksService.updateById(links)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：更新失败
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }


    /**
     * 按status分组查询不同状态下的友链数量
     *
     * @return
     */
    @PostMapping("/admin/selectLinksListByStatus")
    public ResultBean<List<StatusCount>> selectLinksListByStatus() {
        ResultBean<List<StatusCount>> resultBean = new ResultBean<>();
        QueryWrapper<Links> wrapper;
        ArrayList<StatusCount> statusCounts = new ArrayList<>();
        for (int i = -1; i < 2; i++) {
            wrapper = new QueryWrapper<>();
            if (i == 0) {
                continue;
            }
            wrapper.lambda().eq(Links::getStatus, i).select(Links::getStatus);
            wrapper.lambda().eq(Links::getIsApply,1);
            List<Links> list = iLinksService.list(wrapper);
            StatusCount blogsc = new StatusCount();
            blogsc.setStatus(i);
            blogsc.setCount(list.size());
            statusCounts.add(blogsc);
        }
        resultBean.setData(statusCounts);
        return resultBean;
    }
}
